/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package x10.interfaces.cm15a.receive;

import x10.utilities.ChunkOfBytes;
import x10.enums.Device;
import x10.enums.House;
import x10.enums.Interfaces;
import x10.enums.Remote;
import x10.event.X10Remote;
import x10.utilities.BitSetUtils;

/**
 *
 * @author Denny
 */
public class RFRemoteMessage {
    private ChunkOfBytes bytes;
    private int remoteCommandByte = 3;
    private int addressByte = 4;

    public RFRemoteMessage(ChunkOfBytes bytes){
        this.bytes = bytes;

        //DEBUG
        //System.out.println("RAW RF: " + bytes.toString());
    }

    public X10Remote parse(){
        X10Remote remote = new X10Remote(getHouseCode(), getDeviceCode(), getRemoteCommand(), Interfaces.CM15A.toString());
        return remote;
    }

    public House getHouseCode(){
        byte address = bytes.getBytes()[addressByte];
        byte nibble = BitSetUtils.getNibble(address, "UPPER", "LOWER");
        return House.getHouseFromRadioFrequency((int)nibble);
    }

    //Note: not sure at this point if the unit code is valid for a remote command
    //The remote receiver is supposed to only monitor the house code.  In order
    //to properly track the remote command in the command queue, we're going to
    //parse the lower address nibble to its actual int value, which should be 0.
    //Normally, the Unit code would be actual int value + 1, to put it into the
    //1 - 16 address space of the unit codes (vs 0 - 15).  Not adding 1 at this time has the
    //benefit of returning a 0, which would mean that remote codes would be added
    //to the command queue with address A0, B0, C0, etc., depending on the house code.

    public Device getDeviceCode(){
        byte address = bytes.getBytes()[addressByte];
        byte nibble = BitSetUtils.getNibble(address, "LOWER", "LOWER");
        return Device.getDeviceFromOrdinal(1 + (int)nibble);
    }

    public Remote getRemoteCommand(){
        return Remote.getRemote((int)bytes.getBytes()[remoteCommandByte]);
    }
}
